<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BCooker
 *
 * @author jianfeng
 */
class BCooker {

    const Owner_Type_Entity = 'entity';
    const Owner_Type_TreeTable = 'treeTable';
    const Operation_Admin = 'admin';
    const Operation_Manager = 'manager';
    const Operation_member = 'member';
    const Operation_Manage = 'manage';
    const Operation_Client = 'client';

    //put your code here
    /**
     *
     * @var BUILink
     */
    public $activeLink;

    /**
     *
     * @var BUIControl
     */
    public $sender;

    /**
     *
     * @var BUIControl
     */
    private $updateControl;

    /**
     *
     * @var BPostManager
     */
    public $postManager;

    /**
     *
     * @var type 
     */
    public $postData;

    /**
     * 
     * @param type $data
     */
    protected $operationMap = array();

    public function __construct($data = NULL) {
        $this->activeLink = Blyn::app()->getUIManager()->getActiveLink();
        $this->sender = Blyn::app()->getUIManager()->getSenderControl();
        //$this->updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        //$this->updateControl = $this->getCookerControl();
        $this->postManager = new BPostManager();
        $this->postData = $this->postManager->parsedModelData();

        $this->init($data);
    }

    public function init($data = NULL) {
        
    }

    public function __get($name) {
        switch ($name) {
            case 'updateControl':
                return $this->getCookerControl();
        }
    }

    protected function getOwnerFromActiveLinks($data = NULL) {
        $ownerType = $this->activeLink->getLinkVariable('ownerType');
        $ownerId = $this->activeLink->getLinkVariable('ownerId');
        $ownerClass = $this->activeLink->getLinkVariable('ownerClass');
        $ownerTableName = $this->activeLink->getLinkVariable('ownerTable');

        if ($ownerType == NULL)
            $ownerType = self::Owner_Type_Entry;


        $owner = NULL;
        if ($ownerType == self::Owner_Type_Entity) {
            $owner = new $ownerClass($ownerId);
        }

        if ($ownerType == self::Owner_Type_TreeTable) {
            $ownerTableClass = 'BASheet'; //default tableClass
            if ($ownerClass != NULL) {
                $ownerTableClass = $ownerClass;
            }

            if ($ownerTableName != NULL) {
                $ownerTable = new $ownerTableClass($ownerTableName);
            } else {
                $ownerTable = new $ownerTableClass();
            }

            if ($ownerTable instanceof BTreeTable && $ownerId > 0) {
                $owner = $ownerTable->getListById($ownerId);
            }
        }
        return $owner;
    }

    public function getCookerOprationList($data = NULL) {

        return array(array(
                self::Operation_Admin => self::Operation_Admin,
                self::Operation_Manager => self::Operation_Manager,
                self::Operation_member => self::Operation_member
        ));
    }

    public function getCookerOperationName($operation = NULL) {

        $mapTable = new BASheet('cookerOperation');

        if ($operation == NULL)
            $operation = Blyn::app()->getActiveOperation();

        $itemData = array();
        if ($operation instanceof BOperation) {
            $operationName = $operation->getName();
        }

        if (is_string($operation)) {
            $operationName = $operation;
        }

        if (isset($operationName) && $operationName != NULL) {
            $itemData['cookerClass'] = get_class($this);
            $itemData['activeOpeation'] = $operation;

            $list = $mapTable->getList(array('itemData' => $itemData));

            if ($list != NULL) {
                $opName = $list->getItem('cookerOperation')->getName();
                return $opName;
            }
        }
        return FALSE;
    }

    public function addOperationMatchMap($cookerOperationName, $operation = NULL) {

        $cookerSupportOperationMapTable = new BASheet('cookerOperation');

        if ($operation == NULL) {
            $operation = Blyn::app()->getActiveOperation();
        }

        $itemData = array();
        if (is_string($operation))
            $operationName = $operation;
        if ($operation instanceof BOperation) {
            $operationName = $operation->getName();
        }
        if ($cookerOperationName != NULL && $operationName != NULL) {
            $itemData['cookerClass'] = get_class($this);
            $itemData['cookerOperation'] = $cookerOperationName;
            $itemData['actionOperation'] = $operationName;

            $list = $cookerSupportOperationMapTable->getList(array('itemData' => $itemData));

            if ($list == NULL) {
                $cookerSupportOperationMapTable->addList($itemData);
            }
        }
    }

    public function getUpdateControl() {
        return $this->getCookerControl();
    }

    public function getCookerControl($control = NULL) {

        if ($control instanceof BUIControl) {
            $control->setViewTemplate('cooker.' . $this->getCookerName());
            return $control;
        }

        if ($this->updateControl == NULL) {
            $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
            if ($updateControl instanceof BUIScreen) {
                $control = new BUIThemedControl($this->getCookerName());

                $updateControl->addChildControl($control);
                $updateControl = $control;
            }
            $this->updateControl = $updateControl;
        }
        //if ($this->updateControl->getViewPath() == NULL) {
        $this->updateControl->setViewTemplate('cooker.' . $this->getCookerName());
        //}

        return $this->updateControl;
    }

    public function getCookerViewPath($relativePath = TRUE) {

        $cookerName = $this->getCookerName();

        return $cookerName;
    }

    public function getViewTemplate() {
        return 'cooker.' . $this->getCookerName();
    }

    public function getCookerName() {
        return BParseTextUtil::parseText(BParseTextUtil::Todo_Remove_Capital_Prefix_And_Specific_Surfix, get_class($this), array('surfix' => 'Cooker'));
    }

}
